﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.PersonService;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;
using System.Web.UI.HtmlControls;

namespace RN.ERP.Web.Exam
{
    public partial class BmKCInfo : BasePage
    {
        PersonServiceClient client = new PersonServiceClient();
        private string QCPageCode = "QC";
        private string QCPage = "BmQCList.aspx";
        private string KMPageCode = "KM";
        private string KMPage = "BmKMList.aspx";
        public string AppID
        {
            get
            {
                return HttpContext.Current.Application[ApplicationConstant.APPLICATION_ID].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ////type:0添加1查看2修改3复制考场
                hidType.Value = Request.QueryString["type"];
                ////考场id
                hidKCId.Value = Request.QueryString["kcid"];

                if (hidType.Value == "1")
                {
                    lbtnSave.Visible = false;
                }
                ////绑定期次下拉
                BindQCDDL();
                ////绑定科目下拉
                BindKMDDL();
                ////绑定已有数据
                BindData();
                ////绑定考生范围
                GetCity();
            }
        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            ////考生范围
            string code = string.Empty;
            for (int j = 0; j < rptCity.Items.Count; j++)
            {
                HtmlInputCheckBox cbox = (HtmlInputCheckBox)rptCity.Items[j].FindControl("ckbChoose");
                if (cbox.Checked)
                {
                    code += cbox.Value + ",";
                }
            }
            if (string.IsNullOrEmpty(code) || string.IsNullOrEmpty(ddlKM.SelectedValue) || string.IsNullOrEmpty(ddlQC.SelectedValue))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('请完善信息！');", true);
            }
            else
            {
                Exam_Room examKC = new Exam_Room();
                OperationResult result = new OperationResult();
                result.Result = false;
                result.Message = "操作失败";
                if (!string.IsNullOrEmpty(hidKCId.Value) && hidType.Value == "2")
                {
                    ////修改考场
                    int kcId = 0;
                    int.TryParse(hidKCId.Value, out kcId);
                    examKC.ID = kcId;
                }
                examKC.Title = txtTitle.Text;
                examKC.Code = txtCode.Text;
                examKC.Number = int.Parse(txtNumber.Text);
                examKC.QCId = int.Parse(ddlQC.SelectedValue);
                examKC.ExamDate = DateTime.Parse(txtExamDate.Text);
                examKC.ExamAddress = txtAddress.Text;
                examKC.KMId = int.Parse(ddlKM.SelectedValue);
                examKC.CityCode = code.TrimEnd(',');
                examKC.IsrtDate = DateTime.Now;
                examKC.IsDel = false;
                var kc = JsonConvert.SerializeObject(examKC);
                if (hidType.Value == "0" || hidType.Value == "3")
                {
                    ////添加或复制添加考场
                    result = JsonConvert.DeserializeObject<ModelService.OperationResult>(client.AddExamKC(kc));
                }
                else if (hidType.Value == "2")
                {
                    ////修改考场
                    result = JsonConvert.DeserializeObject<ModelService.OperationResult>(client.EditExamKC(kc));
                }
                if (result.Result)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "','',CloseWin);", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "');", true);
                }
            }
        }

        protected void rptCity_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (!string.IsNullOrEmpty(hidCity.Value))
            {
                string[] str = hidCity.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); ;
                HtmlInputCheckBox cbox = (HtmlInputCheckBox)e.Item.FindControl("ckbChoose");
                if (str.Contains(cbox.Value))
                {
                    cbox.Checked = true;
                }
            }
        }

        protected void ddlQC_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindKMDDL();
        }

        private void BindData()
        {
            if (!string.IsNullOrEmpty(hidKCId.Value))
            {
                int kcId = 0;
                int.TryParse(hidKCId.Value, out kcId);
                Exam_Room examKC = JsonConvert.DeserializeObject<Exam_Room>(client.GetExamKC(kcId));
                if (examKC != null)
                {
                    txtTitle.Text = examKC.Title;
                    txtCode.Text = examKC.Code;
                    txtNumber.Text = examKC.Number.ToString();
                    ddlQC.SelectedValue = examKC.QCId.ToString();
                    BindKMDDL();
                    txtExamDate.Text = examKC.ExamDate.ToString();
                    txtAddress.Text = examKC.ExamAddress;
                    ddlKM.SelectedValue = examKC.KMId.ToString();
                    hidCity.Value = examKC.CityCode;
                }
            }
        }

        private void BindQCDDL()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                int count = 0;
                ddlQC.DataSource = JsonConvert.DeserializeObject<List<ModelService.Exam_QC>>(client.GetExamQCList
                    (QCPageCode, UserInfo.RoleId, QCPage, AppID, 0, 0, out count));
                ddlQC.DataValueField = "ID";
                ddlQC.DataTextField = "Title";
                ddlQC.DataBind();
                ddlQC.Items.Insert(0, new ListItem("--请选择--", ""));
            }
        }

        private void BindKMDDL()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                int count = 0;
                ddlKM.DataSource = JsonConvert.DeserializeObject<List<ModelService.Exam_KM>>(client.GetExamKMList
                    (KMPageCode, UserInfo.RoleId, KMPage, AppID, ddlQC.SelectedValue, 0, 0, out count));
                ddlKM.DataValueField = "ID";
                ddlKM.DataTextField = "Title";
                ddlKM.DataBind();
                ddlKM.Items.Insert(0, new ListItem("--请选择--", ""));
            }
        }

        private void GetCity()
        {
            //string proStr = Application[ApplicationConstant.APPLICATION_AREA].ToString();
            //List<a_AreaInfo> list = JsonConvert.DeserializeObject<List<a_AreaInfo>>(proStr);
            List<T_Sys_Dict> list = new List<T_Sys_Dict>();
            using (BasicService.BasicServiceClient bs = new BasicService.BasicServiceClient())
            {
                string rst = bs.GetExpertDBList();
                list = JsonConvert.DeserializeObject<List<T_Sys_Dict>>(rst) ?? new List<T_Sys_Dict>();
                list = list.Where(a => a.ParentSID == 0).ToList() ;
            }
            rptCity.DataSource = list;
            rptCity.DataBind();
        }
    }
}